<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            Layui的树形表格treeTable，支持异步加载(懒加载)、复选框联动、折叠状态记忆。<br>
        </blockquote>
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加单位 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                </div>
            </script>

            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>

<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="save">新增</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/jquery-1.12.1.min.js"></script>
<script src="/js/common.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable','layer','form'], function () {
        var $ = layui.jquery;
        var form=layui.form;
        var layer=layui.layer;
        var table = layui.table;
        var treetable = layui.treetable;

        let dataObj=api.getDeptModelList();


        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            data:dataObj.data,
            toolbar: '#toolbarDemo',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'deptName', minWidth: 200, title: '部门名称'},
                {field: 'deptNo', title: '部门编号',width: 200},
                {field: 'deptDesc', title: '部门介绍',width: 200},
                {
                    field: 'pid', width: 150, align: 'center', templet: function (d) {
                        if (d.pid ===0) {
                            return '<span class="layui-badge layui-bg-gray">单位</span>';
                        }else {
                            return '<span class="layui-badge layui-bg-blue">科室</span>';
                        }
                    }, title: '部门类型'
                },
                {
                    field: 'deptType', width: 150, align: 'center', templet: function (d) {
                        if (d.deptType ===1) {
                            return '<span class="layui-badge layui-bg-gray">特殊部门</span>';
                        }else if(d.deptType ===2){
                            return '<span class="layui-badge layui-bg-gray">职能部门</span>';
                        }else {
                            return '';
                        }
                    }, title: '部门类型'
                },
                {field: 'createdate', title: '部门创建时间',width: 200},
                {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(munu-table)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加单位',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../../page/dept/addparentdept.html',
                    end:function(){
                        location.reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            console.log(JSON.stringify(data))
            if (layEvent === 'del') {
                layer.msg('删除' + data.id);
            } else if (layEvent === 'edit') {
                console.log("编辑===>")
                update(data)
            }else if (layEvent === 'save') {
                console.log("编辑===>")
                save(data);
            }
        });


        function save(data) {
            var index = layer.open({
                title: '添加科室',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/dept/addchilddept.html?obj='+data.id,
                end:function(){
                    location.reload();
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }

        function update(data){
            console.log(JSON.stringify(data))
            var index = layer.open({
                title: '编辑部门',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '../../page/dept/updatedept.html?obj='+data.id,
                end:function(){
                    location.reload();
                }
            });
        }

        form.on('submit(formDemo)', function (data) {
            console.log(JSON.stringify(data))
            let obj=api.saveMenuModel(data.field)
            layer.msg(JSON.stringify(obj))
            layer.close(index);
        })
    });
</script>
</body>
</html>